package com.wjs.blog.service.Imp;

import com.alibaba.fastjson.JSON;
import com.mysql.cj.util.StringUtils;
import com.wjs.blog.dao.pojo.SysUser;
import com.wjs.blog.service.LoginService;
import com.wjs.blog.service.SysUserService;
import com.wjs.blog.util.JWTUtils;
import com.wjs.blog.vo.ErrorCode;
import com.wjs.blog.vo.Result;
import com.wjs.blog.vo.parms.LoginParms;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImp implements LoginService {
    private static final String slat = "mszlu!@#";
    @Autowired
    SysUserService sysUserService;
    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public Result login(LoginParms loginParms) {
        String account = loginParms.getAccount();
        String password = loginParms.getPassword();
        if (StringUtils.isNullOrEmpty(account) || StringUtils.isNullOrEmpty(password)) {
            return Result.fail(ErrorCode.PARAMS_ERROR.getCode(), ErrorCode.PARAMS_ERROR.getMsg());
        }
        password = DigestUtils.md5Hex(password + slat);
        System.out.println(password);
        SysUser user = sysUserService.findUser(account, password);
        if (user == null) {
            return Result.fail(ErrorCode.ACCOUNT_PWD_NOT_EXIST.getCode(), ErrorCode.ACCOUNT_PWD_NOT_EXIST.getMsg());
        }
        String token = JWTUtils.createToken(user.getId());
        redisTemplate.opsForValue().set("token_" + token, JSON.toJSONString(user), 1, TimeUnit.DAYS);
        return Result.success(token);
    }

    @Override
    public SysUser checktoken(String token) {
        if (StringUtils.isNullOrEmpty(token)) {
            return null;
        }
        Map<String, Object> map = JWTUtils.checkToken(token);
        if (map == null) {
            return null;
        }
        String userJson = (String) redisTemplate.opsForValue().get("token_" + token);
        if (StringUtils.isNullOrEmpty(userJson)) {
            return null;
        }
        SysUser sysUser = JSON.parseObject(userJson, SysUser.class);
        return sysUser;
    }

    @Override
    public Result loginout(String token) {
        redisTemplate.delete("token_" + token);
        return Result.success(null);
    }

    @Override
    public Result register(LoginParms loginParms) {
        String account = loginParms.getAccount();
        String password = loginParms.getPassword();
        String nickname = loginParms.getNickname();
        if (StringUtils.isNullOrEmpty(account)
                || StringUtils.isNullOrEmpty(password)
                || StringUtils.isNullOrEmpty(nickname)
        ) {
            return Result.fail(ErrorCode.PARAMS_ERROR.getCode(), ErrorCode.PARAMS_ERROR.getMsg());
        }
        SysUser sysUser = this.sysUserService.findUserByAccount(account);
        if(sysUser!=null){
            return Result.fail(ErrorCode.ACCOUNT_EXIST.getCode(), ErrorCode.ACCOUNT_EXIST.getMsg());
        }
        sysUser=new SysUser();
        sysUser.setNickname(nickname);
        sysUser.setAccount(account);
        sysUser.setPassword(DigestUtils.md5Hex(password+slat));
        sysUser.setCreateDate(System.currentTimeMillis());
        sysUser.setLastLogin(System.currentTimeMillis());
        sysUser.setAvatar("/static/img/logo.b3a48c0.png");
        sysUser.setAdmin(1); //1 为true
        sysUser.setDeleted(0); // 0 为false
        sysUser.setSalt("");
        sysUser.setStatus("");
        sysUser.setEmail("");
        this.sysUserService.save(sysUser);
        String token = JWTUtils.createToken(sysUser.getId());
        redisTemplate.opsForValue().set("token_" + token, JSON.toJSONString(sysUser), 1, TimeUnit.DAYS);
        return Result.success(token);

    }
}
